﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using AjaxControlToolkit;
using posttracker.Classes;

namespace posttracker
{

    public partial class Navigation : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Button btnSearchTrackNumInfo = (Button)cphParcelID.FindControl("btnSearchTrackNumInfo");
            btnSearchTrackNumInfo.Click += btnSearchTrackNumInfo_Click;
        }


        protected void btnSearchTrackNumInfo_Click(object sender, EventArgs e)
        {
            TextBox txtEnterTrackNum = (TextBox)cphParcelID.FindControl("txtEnterTrackNum");
            DropDownList ddlCountry = (DropDownList)cphParcelID.FindControl("ddlCountry");

            IList<Country> lstCountry = Loader.GetCountry();

            try
            {
                var lst = Loader.GetGuestTrackNumbersInfo(txtEnterTrackNum.Text, lstCountry[ddlCountry.SelectedIndex].CountryID);
               
                if (!Loader.isLogged())
                {
                    mvContentID.ActiveViewIndex = 1;
                    // при добавлении в базу этого вызова не надо
                    BuildAccordion(lst);
                }
                else
                {
                    //Loader.AddNewDataInDB(lst);
                    mvContentID.ActiveViewIndex = 1;
                }


            }
            catch (WebException ex)
            {
                if (ex.Message.IndexOf("(406)") > 0)
                {
                    txtEnterTrackNum.Text = "В доступе отказано";
                }
            }
            catch (NumberIsExistsException ex)
            {
                txtEnterTrackNum.Text = ex.Message;
            }

        }

        /////////////////////////////////////////////////////////////////////////////////////////////
        #region Вспомогательные методы

        private void RenderUserTrackNumInfo()
        {
            Button btnSearchTrackNumInfo = (Button)cphParcelID.FindControl("btnSearchTrackNumInfo");

            Accordion accTrackNembersInfo = (Accordion)cphParcelID.FindControl("accTrackNembersInfo");

            if (!Loader.isLogged())
            {
                btnSearchTrackNumInfo.Text = "Найти";
            }
            else
            {
                btnSearchTrackNumInfo.Text = "Добавить";

                int userID = Loader.GetUserID();

                if (userID >= 0)
                {
                    IList<TrackNumbers> lst = Loader.GetTrackNumbersInfo(userID);

                    BuildAccordion(lst);

                }
            }
        }

        private void BuildAccordion(IList<TrackNumbers> aList)
        {
            Accordion accTrackNembersInfo = (Accordion)cphParcelID.FindControl("accTrackNembersInfo");
            accTrackNembersInfo.Panes.Clear();

            for (int i = 0; i < aList.Count; i++)
            {
                AccordionPane ap = new AccordionPane();
                ap.ID = "pane_" + Guid.NewGuid(); // aList[i].TrackNumber.ToUpper();
                ap.HeaderContainer.Controls.Add(new LiteralControl(aList[i].TrackNumber.ToUpper()));

                IList<Record> lstRec = aList[i].ListRecords;


                Table table = new Table();


                for (int j = 0; j < lstRec.Count(); j++)
                {
                    TableRow row = new TableRow();
                    table.Rows.Add(row);

                    TableCell cell = new TableCell();

                    cell.Text = lstRec[j].DateTime.ToShortDateString();
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Width = 450;
                    cell.Text = lstRec[j].Event;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = lstRec[j].PostService;
                    row.Cells.Add(cell);
                }

                if (lstRec.Count() > 0)
                {
                    ap.ContentContainer.Controls.Add(table);
                }


                accTrackNembersInfo.Panes.Add(ap);
            }
        }

        #endregion


        protected void btnHome_Click(object sender, EventArgs e)
        {
            mvContentID.ActiveViewIndex = 0;

            SetInSessionActiveView(mvContentID.ActiveViewIndex);
        }

        protected void btnParcel_Click(object sender, EventArgs e)
        {
            mvContentID.ActiveViewIndex = 1;

            SetInSessionActiveView(mvContentID.ActiveViewIndex);
        }

        protected void btnArhParcel_Click(object sender, EventArgs e)
        {
            mvContentID.ActiveViewIndex = 2;

            SetInSessionActiveView(mvContentID.ActiveViewIndex);
        }

        protected void btnSettings_Click(object sender, EventArgs e)
        {
            mvContentID.ActiveViewIndex = 3;

            SetInSessionActiveView(mvContentID.ActiveViewIndex);
        }

        protected void btnStatistic_Click(object sender, EventArgs e)
        {
            mvContentID.ActiveViewIndex = 4;

            SetInSessionActiveView(mvContentID.ActiveViewIndex);
        }

        protected override void OnPreRender(EventArgs e)
        {
            SetSelectedStyle(mvContentID.ActiveViewIndex);

            switch (mvContentID.ActiveViewIndex)
            {
                case 1:
                    RenderUserTrackNumInfo();
                    break;
                case 2:

                    break;
                case 3:

                    break;
                case 4:

                    break;
            }


            base.OnPreRender(e);
        }

        protected void SetSelectedStyle(int index)
        {
            List<object> ListBtns;
            if (!Loader.isLogged())
            {
                BtnArhParcel.Visible = false;
                BtnSettings.Visible = false;
                BtnStatistic.Visible = false;
                ListBtns = new List<object>(new object[] { BtnHome, BtnParcel });
            }
            else
            {
                ListBtns = new List<object>(new object[] { BtnHome, BtnParcel, BtnArhParcel, BtnSettings, BtnStatistic });
            }

            switch (index)
            {
                case 0:
                    //btnHome
                    BtnHome.Style.Add("background-size", "85%");
                    BtnHome.Style.Add("width", "115%");
                    BtnHome.Style.Add("background-color", "#de2200");
                    ListBtns.Remove(BtnHome);

                    break;
                case 1:
                    //btnParcel
                    BtnParcel.Style.Add("background-size", "95%");
                    BtnParcel.Style.Add("width", "115%");
                    BtnParcel.Style.Add("background-color", "#0062d1");
                    ListBtns.Remove(BtnParcel);

                    break;
                case 2:
                    //btnArhParcel
                    BtnArhParcel.Style.Add("background-size", "105%");
                    BtnArhParcel.Style.Add("width", "115%");
                    BtnArhParcel.Style.Add("background-color", "#00b104");
                    ListBtns.Remove(BtnArhParcel);
                    break;
                case 3:
                    //btnSettings
                    BtnSettings.Style.Add("background-size", "95%");
                    BtnSettings.Style.Add("width", "115%");
                    BtnSettings.Style.Add("background-color", "#8900d7");
                    ListBtns.Remove(BtnSettings);
                    break;
                case 4:
                    //btnStatistic
                    BtnStatistic.Style.Add("background-size", "95%");
                    BtnStatistic.Style.Add("width", "115%");
                    BtnStatistic.Style.Add("background-color", "#e29000");
                    ListBtns.Remove(BtnStatistic);

                    break;

            }

            foreach (Object obj in ListBtns)
            {
                if (obj is Button)
                {
                    Button ctrBtn = (Button)obj;
                    ctrBtn.Style.Clear();
                    ctrBtn.CssClass = "nav" + ctrBtn.ID;
                }

            }

        }



        #region Вспомогательные методы

        private void SetInSessionActiveView(int aIndex)
        {
            if (Session["ActiveViewIndex"] != null)
            {
                Session["ActiveViewIndex"] = aIndex;
            }
            else
            {
                Session.Add("ActiveViewIndex", aIndex);
            }
        }

        #endregion
    }
}